/**
 * \file Namespace.h
 * \brief Namespace information management facilities
 * \author Corey Tabaka
 */

/*
   Copyright 2006 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#ifndef __KERNEL_NAMESPACE_H__
#define __KERNEL_NAMESPACE_H__

#include <ktypes.h>

/**
 * Variable flags.
 */
enum {
	VF_SYSTEM=0x01,			// value can only be changed by the system
	VF_PERSISTENT=0x02,		// value is stored in a config file
	VF_USER=0x10,			// created by the user
	VF_TEMP=0x20,			// temporary variables, also kernel params
	VF_HEAP=0x40,			// var may be freed by the namespace
};

class Var;

class Namespace {
	protected:
	
	Namespace(void);
	~Namespace(void);
	
	static bool syscallHandler(regs_t *regs);
	
	public:
	
	static void initialize(void);
	
	static bool isKey(const char *key);
	static void create(const char *key, long long value, int flags=0);
	static void create(const char *key, const char *str, int flags=0);
	
	static const char *getString(const char *key);
	static long long getInt(const char *key);
	
	static bool set(const char *key, long long value);
	static bool set(const char *key, const char *value);
	
	static void registerVar(Var &var);
	static void unregisterVar(Var &var);
	
	static void dumpVars(const char *prefix=0);
};

class VarListener {
	public:
	
	VarListener(void) {}
	virtual ~VarListener(void) {}
	
	virtual bool set(Var &var, const char *str) = 0;
	virtual bool set(Var &var, long long value) = 0;
	
	virtual bool get(Var &var);
};

class Var {
	private:
	
	Var *	next;
	char *	name;
	int		flags;
	
	VarListener *listener;
	
	public:

	Var(const char *name, long long ivalue=0, int flags=0);
	Var(const char *name, const char *svalue, int flags=0);
	~Var(void);
	
	void set(const char *svalue);
	void set(long long ivalue);
	
	long long operator= ( const long long ivalue );
	void operator= ( const char *svalue );
	
	void setListener(VarListener *listener);

	long long	ivalue;
	char *		svalue;
	
	// TODO: add an interface pointer to allow modules to register a set
	// of accessor methods (get,set) that get called before returning the
	// ivalue/svalue fields
	
	friend class Namespace;
	friend class VarListener;
};

#endif
